package com.roadmap.repo.db;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.hibernate.Session;

import com.roadmap.common.BaseEntity;
import com.roadmap.exception.InvalidActionException;
import com.roadmap.exception.InvalidQueryException;
import com.roadmap.exception.InvalidValueException;
import com.roadmap.exception.NullObjectException;
import com.roadmap.exception.ServerException;

public interface IDatabaseUtility {
	public Session getSession() throws ServerException;

	/** save an entity to database */
	public void save(BaseEntity entity) throws ServerException,
			NullObjectException, InvalidActionException;

	/** update an entity in database */
	public void update(BaseEntity entity) throws ServerException,
			NullObjectException, InvalidActionException;

	/** delete an entity from database */
	public void delete(BaseEntity entity) throws ServerException,
			NullObjectException, InvalidActionException;

	/** delete an entity from database */
	// public void delete(String entityName, String criterial) throws
	// ServerException,
	// NullObjectException, InvalidActionException;
	/** save or update several entities at one time */
	public void saveOrUpdateEntities(Vector<BaseEntity> entities)
			throws ServerException, NullObjectException;

	public void saveOrUpdateEntity(BaseEntity entity) throws ServerException,
			NullObjectException;

	/**
	 * get an entity from database by its ID. return null if nothing is found
	 * 
	 * @param clazz:
	 *            should be the type of entities which are mapped to tables in
	 *            database
	 * @param id:
	 *            the id of entity you want to get from database
	 */
	public BaseEntity getEntityById(Class clazz, String id)
			throws ServerException, NullObjectException, InvalidValueException,
			InvalidQueryException;

	/**
	 * get a set of entities by a specific query criteria
	 * 
	 * @param clazz:
	 *            the sub-classes of BaseEntity, e.g. Aspirant.class
	 * @param criteria:
	 *            the query condition, e.g. mail='chen@123.com'
	 */
	public List getEntitiesByCriteria(Class clazz, String criteria)
			throws ServerException, NullObjectException, InvalidValueException,
			InvalidQueryException;

	/** search in database by using HSQL and return a list of entities */
	@Deprecated
	public List queryByHSQL(String hsql) throws ServerException,
			NullObjectException, InvalidQueryException;

	/** search in database by using native SQL and return a list of entities */
	public List queryBySQL(String sql) throws ServerException,
			NullObjectException, InvalidQueryException;

	/** anther query way. e.g. from LifePlan where aspirantId=? */
	public List executeHQLQuery(String hql, Object[] params)
			throws ServerException;

	public List executeHQLQuery(String hql, Object[] params, int maxResults)
			throws ServerException;

	/** create a native query */
	public RdcQuery createNativeSQLQuery(String sql) throws ServerException;

	/** return the unique result */
	public Object getUniqueEntity(String hql, Object[] params)
			throws ServerException;

	public List executeHQLQeury(String hql, String alias, String[] paramList)
			throws ServerException;

	/**
	 * all parameters use alias
	 * 
	 * @throws ServerException
	 */
	public List executeHQLQeury(String hql, Map<String, Object> params)
			throws ServerException;
	
	public void executeHQLUpdate(String hql, Object[] params) throws NullObjectException, ServerException;
}
